package com.yimi.policyservice.repository;

import com.yimi.policyservice.entity.base.optr.OptrComp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CompRepositroy extends JpaRepository<OptrComp,String> {

    /**
     * 删除
     * @param ids
     */
    @Transactional
    @Modifying
    @Query(value = "delete from t_optr_comp where id in ?1",nativeQuery = true)
    void deleteByIdIn(List<String> ids);



    @Query(value = "select count(1) from  t_optr_comp where name=?1 and service_type =?2 ",nativeQuery = true)
    int countByNameAndType(String name,Integer serviceType );
    @Query(value = "select count(1) from  t_optr_comp where name=?1 and service_type =?2 and id !=?3",nativeQuery = true)
    int countByNameAndType(String name,Integer serviceType,String id );

    @Query(value = "  " +
            "select count(*) from (  " +
            "                       select id  " +
            "                       from t_policy_social_product  " +
            "                       where optr_comp_id in (?1)  " +
            "                       union  " +
            "                       select id  " +
            "                       from t_policy_accfund_product  " +
            "                       where optr_comp_id in (?1)  " +
            "                       union  " +
            "                       select id  " +
            "                       from t_policy_social_supp_rule  " +
            "                       where company_id in (?1)  " +
            "                       union  " +
            "                       select id  " +
            "                       from t_policy_accfund_supp_rule  " +
            "                       where company_id in (?1)  " +
            "                       union  " +
            "                       select id  " +
            "                       from t_policy_disabilities_product  " +
            "                       where optr_comp_id in (?1)  " +
            "                     ) temp  " +
            "  " +
            "  ",nativeQuery = true)
    int checkUsed(List<String> ids);
}
